/**
 * JAVACC DEMO 1.0
 */
package com.apache.portal.service.plugin;

import com.apache.api.vo.ResultEntity;
import com.apache.api.vo.ResultMsg;
import com.apache.cache.util.Validator;
import com.apache.portal.common.PortalPlugin;
import com.apache.portal.common.annotion.AnntationBean;
import com.apache.portal.common.util.PortalPubFactory;
import com.apache.rpc.common.LoadRpcService;
import com.apache.tools.DateUtils;
import com.apache.uct.common.LoginUser;
import com.apache.uct.common.ToolsUtil;
import com.apache.uct.common.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * description: member对接插件
 *
 * @author Hou Dayu 创建时间：2016-9-17
 */
@AnntationBean(name = "member")
public class MemberSystemPluginImpl implements PortalPlugin {

    private Logger log = LoggerFactory.getLogger(MemberSystemPluginImpl.class);

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     */
    public Object doInvoke(HttpServletRequest request, HttpServletResponse response,
            Map<String, String> params) {
        String doCode = params.get("doCode");
        ResultMsg msg = null;
        Map<String, Object> paramsMap = new HashMap();
        paramsMap.putAll(params);
        LoginUser loginUser = PortalPubFactory.getInstance().getLoginUser(request);
        if (loginUser != null) {
            paramsMap.put("userId", loginUser.getUserId());
        }
        if ("memberSave".equals(doCode)) {
            return memberSave(request, paramsMap);
        }
        if ("memberInfo".equals(doCode)) {
            return memberInfo(request, paramsMap);
        }
        if ("memberList".equals(doCode)) {
            return memberList(request, paramsMap);
        }
        if ("memberDel".equals(doCode)) {
            return memberDel(request, paramsMap);
        }
        return msg;
    }

    private ResultMsg memberSave(HttpServletRequest request, Map<String, Object> params) {
        ResultMsg msg = new ResultMsg("F", "操作失败！");
        params.put("createTime", DateUtils.Now.fmt_yyyyMMdd_HHmmss());
        ResultEntity entity = LoadRpcService.service()
                .doServiceClient("memberService", "memberSave", params,
                        PortalPubFactory.getInstance().getRpcInfo("member"));
        if ("true".equals(entity.getResult()) && null != entity.getEntity()) {
            msg = new ResultMsg("T", entity.getMessage());
            LoginUser loginUser = (LoginUser) request.getSession().getAttribute("loginUser");

            if (!"manager".equals(loginUser.getUserType())) {
                String memberId = entity.getEntity().toString();
                User user = loginUser.getUser();
                user.setBjcaUserid(memberId);
                user.setUserCname(params.get("memberCname").toString());
                if (!Validator.isEmpty(params.get("mobile"))) {
                    user.setMobile(params.get("mobile").toString());
                }
                loginUser.setUser(user);
                request.getSession().setAttribute("loginUser", loginUser);
            }

        } else {
            msg = new ResultMsg("F", entity.getMessage());
        }
        return msg;
    }

    private Object memberInfo(HttpServletRequest request, Map<String, Object> params) {
        ResultEntity entity = LoadRpcService.service()
                .doServiceClient("memberService", "memberInfo", params,
                        PortalPubFactory.getInstance().getRpcInfo("member"));
        return entity;
    }

    private Object memberList(HttpServletRequest request, Map<String, Object> params) {
        ResultEntity entity = (ResultEntity) LoadRpcService.service()
                .doServiceClient("memberService", "memberList", params,
                        PortalPubFactory.getInstance().getRpcInfo("member"));
        return entity;
    }

    private ResultMsg memberDel(HttpServletRequest request, Map<String, Object> params) {
        ResultMsg msg = new ResultMsg("F", "操作失败！");
        ResultEntity entity = LoadRpcService.service()
                .doServiceClient("memberService", "memberDel", params,
                        PortalPubFactory.getInstance().getRpcInfo("member"));
        if (("true".equals(entity.getResult()))) {
            if (!ToolsUtil.isEmpty(entity.getEntity())) {
                msg = new ResultMsg("T", entity.getMessage());

            } else {
                msg = new ResultMsg("F", entity.getMessage());
            }
        }
        return msg;
    }

}
